package cn.iocoder.yudao.module.erp.service.invoicechecklistitem;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.erp.controller.admin.invoicechecklistitem.vo.InvoiceCheckListItemPageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.invoicechecklistitem.vo.InvoiceCheckListItemSaveReqVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.invoice.InvoiceDO;
import cn.iocoder.yudao.module.erp.dal.dataobject.invoicechecklistitem.InvoiceCheckListItemDO;
import cn.iocoder.yudao.module.erp.dal.dataobject.purpurchaseorder.PurPurchaseOrderDO;
import cn.iocoder.yudao.module.erp.dal.mysql.invoice.InvoiceMapper;
import cn.iocoder.yudao.module.erp.dal.mysql.invoicechecklistitem.InvoiceCheckListItemMapper;
import cn.iocoder.yudao.module.erp.dal.mysql.purpurchaseorder.PurPurchaseOrderMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.erp.enums.ErrorCodeConstants.INVOICE_CHECK_LIST_ITEM_NOT_EXISTS;

/**
 * ERP 采购发票审核单项 Service 实现类
 *
 * @author 源码
 */
@Service
@Validated
public class InvoiceCheckListItemServiceImpl implements InvoiceCheckListItemService {

    @Resource
    private InvoiceCheckListItemMapper invoiceCheckListItemMapper;
    @Resource
    private InvoiceMapper invoiceMapper;
    @Resource
    private PurPurchaseOrderMapper purPurchaseOrderMapper;

    @Override
    public Long createInvoiceCheckListItem(InvoiceCheckListItemSaveReqVO createReqVO) {
        // 插入
        InvoiceCheckListItemDO invoiceCheckListItem = BeanUtils.toBean(createReqVO, InvoiceCheckListItemDO.class);
        invoiceCheckListItemMapper.insert(invoiceCheckListItem);
        // 返回
        return invoiceCheckListItem.getId();
    }

    @Override
    public void updateInvoiceCheckListItem(InvoiceCheckListItemSaveReqVO updateReqVO) {
        // 校验存在
        validateInvoiceCheckListItemExists(updateReqVO.getId());
        // 更新
        InvoiceCheckListItemDO updateObj = BeanUtils.toBean(updateReqVO, InvoiceCheckListItemDO.class);
        invoiceCheckListItemMapper.updateById(updateObj);
    }

    @Override
    public void deleteInvoiceCheckListItem(Long id) {
        // 校验存在
        validateInvoiceCheckListItemExists(id);
        // 删除
        invoiceCheckListItemMapper.deleteById(id);
    }

    private void validateInvoiceCheckListItemExists(Long id) {
        if (invoiceCheckListItemMapper.selectById(id) == null) {
            throw exception(INVOICE_CHECK_LIST_ITEM_NOT_EXISTS);
        }
    }

    @Override
    public InvoiceCheckListItemDO getInvoiceCheckListItem(Long id) {
        return invoiceCheckListItemMapper.selectById(id);
    }

    @Override
    public PageResult<InvoiceCheckListItemDO> getInvoiceCheckListItemPage(InvoiceCheckListItemPageReqVO pageReqVO) {
        return invoiceCheckListItemMapper.selectPage(pageReqVO);
    }

    @Override
    public List<InvoiceDO> getInvoiceCheckListItemByNumbers(List<String> numbers) {
        // 根据订单ids查询订单
        List<PurPurchaseOrderDO> listByIds = purPurchaseOrderMapper.getListByIds(numbers);
        // 拿到整合单id
        List<String> orderIntegrateIds = listByIds.stream().map(PurPurchaseOrderDO -> PurPurchaseOrderDO.getOrderIntegrateId()).collect(Collectors.toList());
        // 根据发票号查询发票
        List<InvoiceDO> invoiceDOS = invoiceMapper.getByOrderIntegrateIds(orderIntegrateIds);
        return invoiceDOS;
    }

    @Override
    public InvoiceDO getNumAndPrice(List<String> numbers) {
        // 根据订单ids查询订单
        List<PurPurchaseOrderDO> listByIds = purPurchaseOrderMapper.getListByIds(numbers);
        InvoiceDO invoiceDO = new InvoiceDO();
        invoiceDO.setOrderIntegrateNum(listByIds.size());
        BigDecimal price = new BigDecimal("0");
        for (PurPurchaseOrderDO purPurchaseOrderDO : listByIds) {
            price = price.add(purPurchaseOrderDO.getTotalPrice());
        }
        invoiceDO.setOrderIntegratePrice(price);
        return invoiceDO;
    }

}